package com.meng.controller;

import com.meng.pojo.Books;
import com.meng.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.awt.print.Book;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {

    @Autowired
    @Qualifier("BookServiceImpl")
    private BookService bookService;
    //    查询全部书籍返货页面
    @RequestMapping("/allBook")
    public String list(Model model){
        List<Books> list =bookService.queryAllBook();
        model.addAttribute("list",list);
        return "allBook";
    }

    //增加书籍页面
    @RequestMapping("/toAddBook")
    public String toAddPaper(){
        return "addBook";
    }

    //添加书籍请求
    @RequestMapping("/addBook")
    public String addBook(Books books){
        System.out.println("addBook===>"+books);
        bookService.addBook(books);
        //添加书籍之后重定向回到aaBook页面
        return "redirect:/book/allBook";
    }

    //跳转到修改页面
    @RequestMapping("/toUpdate")
    public String toUpdatePaper(int id,Model model){
        Books books = bookService.queryBookById(id);
        model.addAttribute("QBook",books);
        System.out.println(id);
        return "updateBook";
    }

    //修改书籍
    @RequestMapping("/update")
    public String updateBook(Books books){
        System.out.println("updateBook=="+books);
        bookService.updateBook(books);
        return "redirect:/book/allBook";
    }

    //删除书籍
    @RequestMapping("/deleteBookById/{bookID}")
    public String deleteBookById(@PathVariable("bookID") int id){
        bookService.deleteBookById(id);
        return "redirect:/book/allBook";
    }

    //根据书籍名称查询
    @RequestMapping("/queryBookByName")
    public String queryBook(String queryBookName, Model model){
        System.out.println("查询书籍：" + queryBookName);
        Books books = bookService.queryBookByName(queryBookName);
        List<Books> list = new ArrayList<Books>();
        list.add(books);
        if (books == null){
            list = bookService.queryAllBook();
            model.addAttribute("error", "没有找到书籍！");
        }
        model.addAttribute("list", list);
        return "queryByName";
    }

    //根据书籍id查询
    @RequestMapping("/queryBookById")
    public String queryBook2(String SbookId, Model model) {
//        System.out.println("查询书籍Id：" + BookId);
//        Books books1 = bookService.queryBookById(BookId);
//        List<Books> list1 = new ArrayList<Books>();
//        list1.add(books1);
//        if (books1 == null){
//            list1 = bookService.queryAllBook();
//            model.addAttribute("error", "没有找到书籍！");
//        }
//        model.addAttribute("list1", list1);
//        return "queryById";
//    }
        Integer BookId = Integer.parseInt(SbookId.trim());
        System.out.println("查询书籍Id：" + BookId);
        Books books1 = bookService.queryBookById(BookId);
        List<Books> list1 = new ArrayList<Books>();
        list1.add(books1);
        if (books1 == null) {
            list1 = bookService.queryAllBook();
            model.addAttribute("error", "没有找到书籍！");
        }
        model.addAttribute("list1", list1);
        return "queryById";
    }
}